#include "opedb.h"
#include<QMessageBox>
#include<QDebug>
#include"QString"

OpeDB::OpeDB(QObject *parent)
    : QObject{parent}
{
    // 添加数据库
    m_db = QSqlDatabase::addDatabase("QSQLITE");
    // 初始化数据库

}



OpeDB &OpeDB::getInstance()
{
    static OpeDB instance;
    return instance;
}

void OpeDB::init()
    {
        m_db.setHostName("localhost");
        m_db.setDatabaseName("E:\\qt\\xiangmu\\tcpServer\\cloud.db");
        if(m_db.open())
        {
            QSqlQuery query;
            query.exec("select * from usrInfo");
            while(query.next())
            {
                QString data = QString("%1,%2,%3").arg(query.value(0).toString())
                .arg(query.value(1).toString())
                .arg(query.value(2).toString());
                qDebug()<< data;
            }
        }
        else
        {
            QMessageBox::critical(NULL,"打开数据库","打开数据库失败");
        }


    }
    OpeDB::~OpeDB()
         {
             m_db.close();
        }


        bool OpeDB::handleRegist(const char *name, const char *pwd)
        {
            if(NULL == name || NULL == pwd )
            {
                qDebug() << "name | pwd is NULL";
                return false;
            }
            QString data = QString("insert into usrInfo(name, pwd) values(\'%1\',\'%2\')").arg(name).arg(pwd);
            qDebug() << data;
            QSqlQuery query;
                return query.exec(data);
        }

        bool OpeDB::handleLogin(const char *name, const char *pwd)
        {

            if(NULL == name || NULL == pwd )
            {
                qDebug() << "name | pwd is NULL";
                return false;
            }
            QString data = QString("select *from usrInfo where name = \'%1\' and pwd = \'%2\' and online = 0").arg(name).arg(pwd);
            qDebug() << data;
            QSqlQuery query;
            query.exec(data);
            if(query.next())
            {
                QString data = QString("update usrInfo set online=1 where name = \'%1\' and pwd = \'%2\' and online = 0").arg(name).arg(pwd);
                qDebug() << data;
                QSqlQuery query;
                query.exec(data);

                return true;
            }
            else
            {
                return false;
            }
        }

        void OpeDB::handleOffine(const char *name)
        {

            if(NULL == name )
            {
                qDebug() << "name is NULL";
                return;
            }
            QString data = QString("update usrInfo set online=0 where name=\'%1\'").arg(name);

            QSqlQuery query;
            query.exec(data);

        }

        QStringList OpeDB::handleAllOnline()
        {
            QString data = QString("select name from usrInfo where online=1");
            QSqlQuery query;
            query.exec(data);
            QStringList result;
            result.clear();

            while  (query.next())
            {
                result.append(query.value(0).toString());

            }
            return result;

        }

        int OpeDB::handleSearchUser(const char *name)
        {

            if(NULL == name)
            {
                return -1;
            }
            QString data = QString("select online from usrInfo where name=\'%1\'").arg(name);
            QSqlQuery query;

            query.exec(data);

            if(query.next())
            {

                int ret =  query.value(0).toInt();
                if(1 == ret)
                {
                    return 1;
                }
                else if(0 == ret)
                {
                    return 0;
                }
            }
            // 用户不存在
            else
            {
                return -1;
            }

        }


int OpeDB::handleAddfriendCheck(const char *friendName, const char *loginName)
{
    if(NULL == friendName || NULL == loginName)
    {
        // 输入内容格式错误
        return -1;
    }
    QString strSql = QString("select id from userInfo where name = '%1'").arg(friendName);
    QSqlQuery query;
    query.exec(strSql);
    if(query.next())
    {
        int friendId = query.value(0).toInt();
        strSql = QString("select id from userInfo where name = '%1'").arg(loginName);
        query.exec(strSql);
        query.next();
        int loginId = query.value(0).toInt();
        strSql = QString("select * from friend where id = '%1' and friendId = '%2'").arg(loginId).arg(friendId);
        query.exec(strSql);
        if(query.next())
        {
            // 如果已经是好友了
            return 0;
        }
        else
        {
            strSql = QString("select online from userInfo where id = '%1'").arg(friendId);
            query.exec(strSql);
            query.next();
            int online = query.value(0).toInt();
            if(online == 1)
            {
                // 对方在线中
                return 1;
            }
            else if(online == 0)
            {
                // 如果对方离线中
                return 2;
            }
        }
    }
    else
    {
        // 用户不存在
        return 3;
    }

}

void OpeDB::handleAddfriend(const char *friendName, const char *loginName)
{
    // 获取好友id
    QString strSql = QString("select id from userInfo where name = '%1'").arg(friendName);
    QSqlQuery query;
    query.exec(strSql);
    query.next();
    int friendId = query.value(0).toInt();
    // 获取登录用户id
    strSql = QString("select id from userInfo where name = '%1'").arg(loginName);
    query.exec(strSql);
    query.next();
    int loginId = query.value(0).toInt();
    // 新增好友
    strSql = QString("insert into friend (id,friendId) values ('%1','%2'), ('%2','%1')").arg(friendId).arg(loginId);
    query.exec(strSql);
}

QStringList OpeDB::handleFlushFriend(const char *name)
{
    QStringList strNameList;
    strNameList.clear();

    if(NULL == name)
    {
        return strNameList;
    }

    QString strSql = QString("select id from userInfo where name = '%1'").arg(name);
    QSqlQuery query;
    query.exec(strSql);
    query.next();
    int loginId = query.value(0).toInt();

    strSql = QString("select friendId from friend where id = '%1'").arg(loginId);
    query.exec(strSql);
    while(query.next())
    {
        int friendId = query.value(0).toInt();
        strSql = QString("select name from userInfo where id = '%1' and online = 1").arg(friendId);
        QSqlQuery nameQuery;
        nameQuery.exec(strSql);
        if(nameQuery.next())
        {
            strNameList.append(nameQuery.value(0).toString());
        }
    }
    return strNameList;
}

void OpeDB::handleDeletefriend(const char *friendName, const char *loginName)
{
    if(NULL == friendName || NULL == loginName)
    {
        return ;
    }
    QString strSql = QString("select id from userInfo where name = '%1'").arg(friendName);
    QSqlQuery query;
    query.exec(strSql);
    query.next();
    int friendId = query.value(0).toInt();
    strSql = QString("select id from userInfo where name = '%1'").arg(loginName);
    query.exec(strSql);
    query.next();
    int userId = query.value(0).toInt();
    strSql = QString("delete from friend where (id = '%1' and friendId = '%2') or (id = '%2' and friendId = '%1')").arg(userId).arg(friendId);
    query.exec(strSql);
}
// #include "opedb.h"
// #include <QMessageBox>
// #include <QDebug>
// #include"QString"
// OpeDB::OpeDB(QObject *parent)
//     : QObject{parent}
// {
//     m_db = QSqlDatabase::addDatabase("QSQLITE");
// }

// OpeDB &OpeDB::getInstance()
// {
//     static OpeDB instance;
//     return instance;
// }

// void OpeDB::init()
// {
//     m_db.setHostName("localhost");
//     m_db.setDatabaseName("E:\\qt\\xiangmu\\tcpServer\\cloud.db");
//     if(m_db.open())
//     {
//         QSqlQuery query;
//         query.exec("select * from usrInfo");
//         while(query.next())
//         {
//             QString data = QString("%1,%2,%3").arg(query.value(0).toString())
//             .arg(query.value(1).toString())
//             .arg(query.value(2).toString());
//             qDebug()<< data;
//         }
//     }
//     else
//     {
//         QMessageBox::critical(NULL,"打开数据库","打开数据库失败");
//     }


// }

// OpeDB::~OpeDB()
// {
//     m_db.close();
// }

// bool OpeDB::handleRegist(const char *name, const char *pwd)
// {
//     if(NULL == name || NULL == pwd )
//     {
//         qDebug() << "name | pwd is NULL";
//         return false;
//     }
//     QString data = QString("insert into usrInfo(name, pwd) values(\'%1\',\'%2\')").arg(name).arg(pwd);
//     qDebug() << data;
//     QSqlQuery query;
//         return query.exec(data);
// }

// bool OpeDB::handlelogin(const char *name, const char *pwd)
// {

//     if(NULL == name || NULL == pwd )
//     {
//         qDebug() << "name | pwd is NULL";
//         return false;
//     }
//     QString data = QString("select *from usrInfo where name = \'%1\' and pwd = \'%2\' and online = 0").arg(name).arg(pwd);
//     qDebug() << data;
//     QSqlQuery query;
//     query.exec(data);
//     if(query.next())
//     {
//         QString data = QString("update usrInfo set online=1 where name = \'%1\' and pwd = \'%2\' and online = 0").arg(name).arg(pwd);
//         qDebug() << data;
//         QSqlQuery query;
//         query.exec(data);

//         return true;
//     }
//     else
//     {
//         return false;
//     }
// }

// void OpeDB::handleOffline(const char *name)
// {

//     if(NULL == name )
//     {
//         qDebug() << "name is NULL";
//         return;
//     }
//     QString data = QString("update usrInfo set online=0 where name=\'%1\'").arg(name);

//     QSqlQuery query;
//     query.exec(data);

// }

// QStringList OpeDB::handleAllOnline()
// {
//     QString data = QString("select name from usrInfo where online=1");
//     QSqlQuery query;
//     query.exec(data);
//     QStringList result;
//     result.clear();

//     while  (query.next())
//     {
//         result.append(query.value(0).toString());

//     }
//     return result;

// }

// int OpeDB::handleSearchUser(const char *name)
// {

//     if(NULL == name)
//     {
//         return -1;
//     }
//     QString data = QString("select online from usrInfo where name=\'%1\'").arg(name);
//     QSqlQuery query;

//     query.exec(data);

//     if(query.next())
//     {

//         int ret =  query.value(0).toInt();
//         if(1 == ret)
//         {
//             return 1;
//         }
//         else if(0 == ret)
//         {
//             return 0;
//         }
//     }
//     // 用户不存在
//     else
//     {
//         return -1;
//     }

// }

// int OpeDB::handleAddfriend(const char *pername, const char *name)
// {
//     if(NULL == pername || NULL == name)
//     {
//         return -1;
//     }
//     QString data = QString("select * from friend where (id=(select id from usrInfo where name=\'%1\')) and friendId = (select id from usrInfo where name=\'%2\')) "
//                            "or (id=(select id from usrInfo where name=\'%3\')) and friendId = (select id from usrInfo where name=\'%4\'))").arg(pername).arg(name).arg(name).arg(pername);
//     qDebug() << data;
//     QSqlQuery query;
//     query.exec(data);
//     if(query.next())
//     {
//         return 0;//双方已是好友

//     }
//     else
//     {
//         data = QString("select online from usrInfo where name=\'%1\'").arg(pername);
//         QSqlQuery query;

//         query.exec(data);

//         if(query.next())
//         {

//             int ret =  query.value(0).toInt();
//             if(1 == ret)
//             {
//                 return 1;//在线
//             }
//             else if (0 == ret)
//             {
//                 return 2;//不在线
//             }
//             else
//             {
//                 return 3;//不存在
//             }
//         }
//         // 用户不存在


//     }

// }

// void OpeDB::handlFlushFriend(const char *name)
// {
//     QStringList strFriendList;
//     strFriendList.clear();
//     if(NULL == name)
//     {
//         return /*strFriendList*/;
//     }
//     QString data = QString("select name from userInfo where online = 1 and id =(select id from friend where friendId=(select id from usrInfo where name=\'%1\'))").arg(name);
//     QSqlQuery query;

//     query.exec(data);
//     while (query.next())
//     {
//         strFriendList.append(query.value(0).toString());
//         qDebug() << query.value(0).toString();
//     }
//     data = QString("select name from userInfo where online = 1 and id =(select friendId from friend where id=(select id from usrInfo where name=\'%1\'))").arg(name);
//     query.exec(data);
//     while (query.next())
//     {
//         strFriendList.append(query.value(0).toString());
//         qDebug() << query.value(0).toString();

//     }
//     return ;//strFriendList;

// }





